/**
 * Created by Harvey Zhao on 2016/8/19.
 */

var url = require('url')
//加密类
var crypto = require("crypto")
var mongoose = require('mongoose')
var shortid = require('shortid')

//站点配置
var settings = require("./Settings")
// var db = mongoose.connect(settings.URL)
console.log('正在连接mongo数据库：mongodb://' + settings.USERNAME + ':*******@' + settings.HOST + ':' + settings.PORT + '/' + settings.DB + '')
var db = mongoose.connect('mongodb://' + settings.USERNAME + ':' + settings.PASSWORD + '@' + settings.HOST + ':' + settings.PORT + '/' + settings.DB + '')
console.log('连接成功！')
//信息删除操作

var DbOpt = {
    del: function (obj, req, res, callback, logMsg) {
        var targetId = req.body._id
        if (shortid.isValid(targetId)) {
            obj.remove({ _id: targetId }, function (err, result) {
                if (err) {
                    res.end(err)
                    return
                } else {
                    console.log(logMsg + " success!")
                    callback && callback('success')
                }
            })
        } else {
            res.end(settings.system_illegal_param)
            return
        }

    },
    findAll: function (obj, req, res, callback, logMsg) {//查找指定对象所有记录
        obj.find({}, function (err, result) {
            if (err) {
                res.end(err)
                return
            } else {
                console.log(logMsg + " success!")
                callback && callback('success', result)
            }
        })
    },
    findOne: function (obj, req, res, callback, logMsg) { //根据ID查找单条记录
        var targetId = req.body._id
        if (shortid.isValid(targetId)) {
            obj.findOne({ _id: targetId }, function (err, result) {
                if (err) {
                    res.end(err)
                    return
                } else {
                    console.log(logMsg + " success!")
                    callback && callback('success', result)
                }
            })
        } else {
            res.end(settings.system_illegal_param)
            return
        }

    },
    updateOneByID: function (obj, req, res, callback, logMsg) {
        var targetId = req.body._id
        if (shortid.isValid(targetId)) {
            var conditions = { _id: targetId }
            req.body.updateDate = new Date()
            req.body._id = null
            var update = { $set: req.body.dto }
            obj.update(conditions, update, function (err, result) {
                if (err) {
                    res.end(err)
                    return
                } else {
                    console.log(logMsg + " success!")
                    callback && callback('success')
                }
            })
        } else {
            res.end(settings.system_illegal_param)
            return
        }

    },
    addOne: function (obj, req, res, callback, logMsg) {
        var newObj = new obj(req.body)
        newObj.save(function (err) {
            if (err) {
                res.end(err)
                return;
            } else {
                console.log(logMsg + " success!")
                callback && callback('success')
            }
        })
    },

    getCount: function (obj, req, res, callback, conditions) { // 查询指定对象的数量
        obj.count(conditions, function (err, count) {
            if (err) {
                res.end(err)
                return;
            } else {
                callback && callback('success', count)
            }

        })
    },
    encrypt: function (data, key) { // 密码加密
        var cipher = crypto.createCipher("bf", key)
        var newPsd = ""
        newPsd += cipher.update(data, "utf8", "hex")
        newPsd += cipher.final("hex")
        return newPsd
    },
    decrypt: function (data, key) { //密码解密
        var decipher = crypto.createDecipher("bf", key)
        var oldPsd = ""
        oldPsd += decipher.update(data, "hex", "utf8")
        oldPsd += decipher.final("utf8")
        return oldPsd
    }
}

module.exports = DbOpt